﻿''' <summary>
''' VB code for the process of returning books and paying late fees.
''' </summary>
''' <author>AJ Whatling, Feng Jiang, Rick Simpson</author>
Partial Class pages_ReturnBooks
    Inherits System.Web.UI.Page

    ''' <summary>
    ''' On page load, fills up table with information about outstanding 
    ''' books (either checked out or reserved) as well as late fees.
    ''' </summary>
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.User.Identity.IsAuthenticated() Then
            PageUtilities.GenerateError("You must login to see the books you can return.", MountainDewOutput)
            Return
        End If

        Dim uname As String = Page.User.Identity.Name

        ' Is the user paying off fees?
        Dim Charge As String = Request.QueryString("charge")
        Dim BookToPay As String = Request.QueryString("bid")

        If Not BookToPay Is Nothing AndAlso Not Charge Is Nothing AndAlso Charge.Equals("return") Then
            LibraryBookDataTier.ReturnBook(BookToPay, uname)
            MountainDewOutput.Text = "Book returned!"
        ElseIf Not BookToPay Is Nothing AndAlso Not Charge Is Nothing AndAlso Charge.Equals("pay") Then
            LibraryBookDataTier.ReturnBook(BookToPay, uname)
            MountainDewOutput.Text = "Book returned and late fee paid!"
        ElseIf Not BookToPay Is Nothing AndAlso Not Charge Is Nothing AndAlso Charge.Equals("renew") Then
            LibraryBookDataTier.RenewBook(BookToPay, uname)
            MountainDewOutput.Text = "Book renewed!"
        End If


        ' Add stuff to HTML table...

        Dim UserName As String = Page.User.Identity.Name.ToString
        Dim UserBooksQuery As String = "SELECT b0.Title, sb0.borrowedDate, sb0.returnDate, sb0.bookID FROM Books b0, ServedBooks sb0 WHERE sb0.bookID=b0.book_ID AND sb0.userID = '" & uname & "'"

        Dim ResultSet As System.Data.OleDb.OleDbDataReader = LibraryBookDataTier.ExecuteSelect(UserBooksQuery)

        Dim TodaysDate As Date = Date.Today

        If Not ResultSet.HasRows Then
            MountainDewOutput.Text &= " You have no books checked out or reserved!"
            Return
        End If

        While ResultSet.Read()
            Dim ContainingRow As WebControls.TableRow = New WebControls.TableRow

            Dim BookTitleCell As WebControls.TableCell = New WebControls.TableCell
            Dim LateFeeCell As WebControls.TableCell = New WebControls.TableCell
            Dim PayOptionCell As WebControls.TableCell = New WebControls.TableCell

            Dim ExpectedReturnDate As Date = Date.Parse(ResultSet(2))

            Dim DaysDiff As Long = DateDiff(DateInterval.Day, ExpectedReturnDate, TodaysDate)

            ' $0.25 for everyday over
            ' test date (10+ months ago): 6/29/2010 12:00:00 AM

            Dim LateFee As Long = Math.Max(0, DaysDiff)
            LateFeeCell.Text = "$" & Math.Round(LateFee, 2)
            If LateFee > 0 Then
                LateFeeCell.Attributes.Add("style", "color: #ff0000")
            End If


            Dim PayOptionLink As WebControls.HyperLink = New WebControls.HyperLink
            Dim Action As String = ""
            If LateFee > 0 Then
                Action = "pay"
            Else
                Action = "return"
            End If
            PayOptionLink.Attributes.Add("href", "ReturnBooks.aspx?charge=" & Action & "&bid=" & ResultSet(3))
            PayOptionLink.Text = Action
            PayOptionCell.Controls.Add(PayOptionLink)

            BookTitleCell.Text = ResultSet(0)

            ' Add "renew" option
            Dim RenewLinkCell As WebControls.TableCell = New WebControls.TableCell
            Dim RenewLink As WebControls.HyperLink = New WebControls.HyperLink
            RenewLink.Attributes.Add("href", "ReturnBooks.aspx?charge=renew&bid=" & ResultSet(3))
            RenewLink.Text = "Renew"
            RenewLinkCell.Controls.Add(RenewLink)

            ' Add to containg row
            ContainingRow.Cells.Add(BookTitleCell)
            ContainingRow.Cells.Add(LateFeeCell)
            ContainingRow.Cells.Add(PayOptionCell)
            ContainingRow.Cells.Add(RenewLinkCell)

            ' Containing row to table
            Me.VaultTable.Rows.Add(ContainingRow)
        End While
    End Sub
End Class
